import { ItemPageVO, Options } from "@/api/examProgress/types";
/**
 * Check if an element has a class
 * @param {HTMLElement} ele
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele: HTMLElement, cls: string) {
  return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}

/**
 * Add class to element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function addClass(ele: HTMLElement, cls: string) {
  if (!hasClass(ele, cls)) ele.className += " " + cls;
}

/**
 * Remove class from element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function removeClass(ele: HTMLElement, cls: string) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
    ele.className = ele.className.replace(reg, " ");
  }
}

/**
 * 判断是否是外部链接
 *
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}

/**
 * 设置Style属性
 *
 * @param propName
 * @param value
 */
export function setStyleProperty(propName: string, value: string) {
  document.documentElement.style.setProperty(propName, value);
}

/**
 * 模糊多个字段查询方法
 *
 * @param list
 * @param queryParamsList
 */
export function moreQueryFilter(list: any[], queryParamsList: any[]) {
  const arr: any = [];
  const dataList = new Map();
  queryParamsList.forEach((v, i) => {
    queryFilter(list, [v]).forEach((j) => {
      if (!dataList.has(j.id)) {
        dataList.set(j.id, j);
      }
    });
  });

  return [...dataList.values()];
}

/**
 * 模糊查询方法
 *
 * @param list
 * @param queryParamsList
 */
export function queryFilter(list: any[], queryParamsList: any[]) {
  console.log("wwwwwwwwwwwwwww", queryParamsList);
  let hitNum = 0;
  queryParamsList.forEach((v) => {
    if (v.value) {
      hitNum++;
    }
  });
  if (hitNum) {
    return list.filter((v: any) => {
      let n = 0;
      queryParamsList.forEach((j: any) => {
        if (fieldJudgment(j.value, v[j.label])) {
          n++;
        }
      });
      return n === hitNum;
    });
  } else {
    return list;
  }
}

/**
 * 模糊对比
 *
 * @param value
 * @param target
 */
export function fieldJudgment(value: String, target: String) {
  const rowTarget = target ? target.toLowerCase() : "";
  if (value && rowTarget.indexOf(value.toLowerCase()) >= 0) {
    return true;
  }
}

/**
 * 表格高度计算
 *
 * @param num
 */
export function computeHeiht(num: any = 120) {
  const dom = document.querySelectorAll(".app-main")[0] as HTMLElement;
  const tableDom = document.querySelectorAll(
    ".customTableClass"
  )[0] as HTMLElement;
  console.log(tableDom, "bbbbbbbbbbbbbbbbbb", dom);
  if (dom) {
    const h: number = dom.offsetHeight;
    console.log(num, "ggggggggggg", h);
    tableDom.style.height = h - num + "px";
    return h - num;
  } else {
    tableDom.style.height = "400px";
  }
  return 400;
}

/**
 * 身份证校验
 *
 */
export function validateIdCard(idCard: any) {
  // 正则表达式规则：18位身份证号码，前17位为数字，最后一位可以是数字或者大写字母X
  const regExp =
    /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
  console.log("fffffffffffff", regExp.test(idCard));
  return regExp.test(idCard);
}

/**
 * 合并计算
 *
 */
export function mergeCalculation(
  data: any,
  rowIndex: number,
  cateName: string
) {
  const arrLength: number[] = []; // 存放secondName对应的数据数组长度
  const cateRows: { rowspan: number; rIndex: number }[] = []; // 存放起始合并行以及合并行数
  data.reduce(
    (
      preValue: { [x: string]: any },
      curValue: { [x: string]: any },
      index: number
    ) => {
      console.log(index, "aaaaa", preValue, cateName, curValue, cateName);
      if (index == 0 || preValue[cateName] != curValue[cateName]) {
        arrLength.push(1);
      } else {
        arrLength[arrLength.length - 1]++;
      }
      return curValue;
    },
    data[0]
  );

  // 获取存放起始合并行以及合并行数
  arrLength.reduce((pre, cur, index, value) => {
    // pre指的是上一次计算过后的prev + cur这个值
    const obj: {
      rIndex: number;
      rowspan: number;
    } = {
      rIndex: pre,
      rowspan: cur,
    };
    console.log("hhhhhhhhhhhhhhh", obj);
    cateRows.push(obj); // rowIndex指的是从第几行开始合并，rowspan指的是合并几行
    return pre + cur;
  }, 0);

  let intRowSpan = 0;
  for (let i = 0; i < arrLength.length; i++) {
    if (cateRows[i].rIndex == rowIndex) {
      intRowSpan = cateRows[i].rowspan;
      break;
    }
  }
  return {
    intRowSpan,
  };
}

/**
 * 判断数据类型
 *
 */
export function isNumber(value: any) {
  return typeof value === "number" && !Number.isNaN(value);
}
